/**
 * 类的泛型支持
 * 静态函数的泛型需要自己去指定类型
 */
class SimpleSet<T> {
  private elements: Map<T, boolean>;
  /**
   * 需要指定一下泛型参数是什么
   * @param elements
   * @returns
   */
  static of<E>(...elements: E[]): SimpleSet<E> {
    const set = new SimpleSet<E>();
    elements.forEach((element) => {});
    return set;
  }
}
// 不传递的时候模式是unknown类型

/**
 * 1. 类是支持结构化类型（鸭子类型的）但是要注意访问修饰符 会触发ts类型检查 public private
 * 2. 在ts中，类即声明值也声明类型，而且类的类型有两种：实例化对象类型，类构造函数类型。
 */

class MyMap {
  state: Record<string, string> = {};
  get(key: string): string | undefined {
    return this.state[key];
  }
  set(key: string, value: string): void {
    this.state[key] = value;
  }
  values(): string[] {
    return Object.values(this.state);
  }
  keys(): string[] {
    return Object.keys(this.state);
  }
  static of(...entries: [string, string][]): MyMap {
    const map = new MyMap();
    entries.forEach((entry) => map.set(entry[0], entry[1]));
    return map;
  }
}
const m1 = new MyMap();
m1.set("id", "1");
m1.set("name", "jack");
console.log(m1.get("name"));
console.log(m1.values());

const m2 = MyMap.of(["name", "rose"], ["sex", "女"]);
console.log(m2.keys());

export {};

/**
 * MyMap实例化类型;
 */

interface MyMap {
  state: Record<string, string>;
  get(key: string): string | undefined;
  set(key: string, value: string): void;
  values(): string[];
  keys(): string[];
}

class Map2 implements MyMap {
  values(): string[] {
    throw new Error("Method not implemented.");
  }
  state: Record<string, string>;
  get(key: unknown): string | undefined {
    throw new Error("Method not implemented.");
  }
  set(key: string, value: string): void;
  set(key: unknown, value: unknown): void {
    throw new Error("Method not implemented.");
  }
  keys(): string[] {
    throw new Error("Method not implemented.");
  }
}

/**
 * MyMap的构造函数类型
 * 静态方法写在构造函数类型接口中
 */
interface MyMapConstructor {
  new (): MyMap;
  of(...entries: [string, string][]): MyMap;
  readonly prototype: MyMap;
}
